package cn.cs.service.impl;


import cn.cs.client.AccountDetailClient;
import cn.cs.client.UserBalanceClient;
import cn.cs.common.Constants;
import cn.cs.common.EmptyUtils;
import cn.cs.common.IdWorker;
import cn.cs.common.Page;
import cn.cs.pojo.AccountDetail;
import cn.cs.pojo.UserBalance;
import cn.cs.service.UserBalanceService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserBalanceServiceImpl implements UserBalanceService {

    @Resource
    private UserBalanceClient userBalanceClient;

    @Resource
    private AccountDetailClient accountDetailClient;


    //根据userId查询账户余额
    @Override
    public UserBalance getUserBalanceById(String userId)throws Exception{
        return userBalanceClient.getUserBalanceById(userId);
    }
    //根据userId查询账户充值明细
    @Override
    public List<AccountDetail> getAccountDetail(String userId) throws Exception {
        return accountDetailClient.getAccountDetail(userId);
    }

    //根据userId 待开发票的资金明细
    @Override
    public List<AccountDetail> getAccountDetailInvoice(String userId) throws Exception {
        return accountDetailClient.getAccountDetailInvoice(userId);
    }
    //充值,往accountDetail添加一条记录 ,并更改账号余额
    @Override
    public Integer addAccountDetail(AccountDetail accountDetail) throws Exception {
        return accountDetailClient.addAccountDetail(accountDetail);
    }

    //账户余额充值业务
    @Transactional
    @Override
    public Integer recharge(AccountDetail accountDetail) throws Exception {
        //获取充值金额
        Double amount = accountDetail.getAmount();
        //获取用户id
        String userId = accountDetail.getUserId();
        //往账户明细表中添加一条记录
        accountDetail.setId(IdWorker.getId());
        accountDetail.setTransactionTime(new Date());
        accountDetailClient.addAccountDetail(accountDetail);
        Map<String,Object> map=new HashMap<String, Object>();
        map.put("userId",userId);
        map.put("balance",amount);
        //根据userId更改用户账户余额
        return userBalanceClient.updateBalance(map);
    }

    @Override
    public List<Map<String,Object>>	getUserBalanceListByMap(Map<String,Object> param)throws Exception{
        return userBalanceClient.getUserBalanceListByMap(param);
    }
    @Override
    public Integer getUserBalanceCountByMap(Map<String,Object> param)throws Exception{
        return userBalanceClient.getUserBalanceCountByMap(param);
    }
    @Override
    public Integer addUserBalance(UserBalance userBalance)throws Exception{
            userBalance.setCreatedTime(new Date());
            return userBalanceClient.addUserBalance(userBalance);
    }

    @Override
    public Integer deleteUserBalanceById(Long id)throws Exception{
        return userBalanceClient.deleteUserBalanceById(id);
    }
    @Override
    public Page<UserBalance> queryUserBalancePageByMap(Map<String,Object> param, Integer pageNo, Integer pageSize)throws Exception{
        Integer total = userBalanceClient.getUserBalanceCountByMap(param);
        pageNo = EmptyUtils.isEmpty(pageNo) ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = EmptyUtils.isEmpty(pageSize) ? Constants.DEFAULT_PAGE_SIZE: pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<Map<String,Object>> userBalanceList = userBalanceClient.getUserBalanceListByMap(param);
        page.setRows(userBalanceList);
        return page;
    }

}
